package com.huixue.cn.ztej.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.huixue.cn.ztej.base.Constant;
import com.huixue.cn.ztej.bean.Mechanical;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.service.SysMechanicaService;
import com.huixue.cn.ztej.service.SysRoleService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.PageParameter;


@Controller
@RequestMapping(value = "/sys/mechanical")
public class MechanicalController extends BaseController {
	
	@Autowired(required = true)
	SysMechanicaService sysMechanicaService;
	
	@RequestMapping(value = "/list")
	public String listMechanica(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			HttpServletRequest request,Model model) 
	{
		
		Map<String, Object> searchParams = new HashMap<String,Object>();
		String searchName = request.getParameter("search_name");		
		if(searchName!=null && searchName.trim().length()>0){
			searchParams.put("mechanicalName",searchName.trim());
			model.addAttribute("mechanicalName", searchName.trim());
		}
		
		Results results = sysMechanicaService.listMechanicalPage(searchParams,page,rows);
		model.addAttribute("res", results);		
		return "/mechan/list";
	}
	
	@RequestMapping(value = "/toUpdate/{mechanical_id}")
	public String toUpdate(@PathVariable ("mechanical_id")Integer mechanical_id, 
			HttpServletRequest request, 
			Model model)
	{
		Mechanical mechanical = sysMechanicaService.findById(mechanical_id);
		Results results = sysMechanicaService.listMechanicalType(-1);
		model.addAttribute("model", mechanical);
		model.addAttribute("res", results);
		return "/mechan/update";
	}
	
	@RequestMapping(value = "/update",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	@ResponseBody
	public Results update(@RequestBody Mechanical mechanical,RedirectAttributes attrs ) {
		Results results = null;
		sysMechanicaService.update(mechanical);
		results = new Results(API_RETURN_STATUS.NORMAL.value(), "修改成功！");
		return results;
	}
	
	@RequestMapping(value = "/toAdd")
	public String toAdd( HttpServletRequest request, Model model)
	{
		Results results = sysMechanicaService.listMechanicalType(-1);
		model.addAttribute("res", results);
		return "/mechan/add";
	}
	
	@RequestMapping(value = "/add",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	@ResponseBody
	public Results add(@RequestBody Mechanical mechanical,RedirectAttributes attrs ) {
		Results results = null;
		sysMechanicaService.saveMechanical(mechanical);
		results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
	
	@RequestMapping(value = "/delete/{mechanical_id}",produces="application/json;charset=UTF-8",method=RequestMethod.DELETE)
	@ResponseBody
	public Results delete(@PathVariable ("mechanical_id")Integer mechanical_id, 
			HttpServletRequest request, 
			Model model)
	{
		sysMechanicaService.delete(mechanical_id);
		Results results = null;
		results = new Results(API_RETURN_STATUS.NORMAL.value(), "删除成功！");
		return results;
	}

}
